﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Romeo.Data;

namespace Romeo.WinForms
{
    public partial class StandardUserInterface : UserControl
    {
        private BestMoveStrategy _Strategy;
        private EvaluationStatistics _FullStats;

        public StandardUserInterface ()
        {
            InitializeComponent ();
            _Strategy = new BestMoveStrategy ();
            _Strategy.MoveSelected += ComputerMoveSelected;
            _Strategy.BestMoveSoFar += ComputerMoveWanted;
            _FullStats = new EvaluationStatistics ();
            _Strategy.MaxLevel = (int)edit_Level.Value;
        }

        private void StartComputerMove ()
        {
            board.OpponentTurn = false;
            _Strategy.Compute (new Board (board.Board, "StartComputerMove"));
        }

        private void board_LegalMovesChanged (object sender, EventArgs e)
        {
            button_Pass.Enabled = board.OpponentCanPass ();
            button_FlipSides.Enabled = board.OpponentTurn;
        }

        private void button_Pass_Click (object sender, EventArgs e)
        {
            board.PlayOn (0, Board.Opponent);
            StartComputerMove ();
        }

        private void ComputerMoveWanted (object sender, CellEventArgs e)
        {
            board.ExpectedCell = e.Cell;
            stats_LastMove.Display (_Strategy);
        }

        private void ComputerMoveSelected (object sender, CellEventArgs e)
        {
            board.PlayOn (e.Cell, Board.Computer);
            stats_LastMove.Display (_Strategy);
            _FullStats.AddStatistics (_Strategy);
            stats_Total.Display (_FullStats);
        }

        private void checkBox_ComputerBlack_CheckedChanged (object sender, EventArgs e)
        {
            board.SwitchSides ();
            board.ComputerPlaysBlack = checkBox_ComputerBlack.Checked;
        }

        private void button_FlipSides_Click (object sender, EventArgs e)
        {
            board.SwitchSides ();
        }

        private void board_ValidCellClicked (object sender, CellEventArgs e)
        {
            StartComputerMove ();
        }

        public void NewGame ()
        {
            board.NewGame ();
            _FullStats.ResetStatistics ();
            stats_LastMove.Display (_FullStats);
            stats_Total.Display (_FullStats);
        }

        private void edit_Level_ValueChanged (object sender, EventArgs e)
        {
            _Strategy.MaxLevel = (int)edit_Level.Value;
        }

    }
}
